package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Classes;
import com.ruoyi.system.service.IClassesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 班级Controller
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/system/classes")
public class ClassesController extends BaseController
{
    @Autowired
    private IClassesService classesService;

    /**
     * 查询班级列表
     */
//    @PreAuthorize("@ss.hasPermi('system:classes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Classes classes)
    {
        startPage();
        List<Classes> list = classesService.selectClassesList(classes);
        return getDataTable(list);
    }

    /**
     * 用户上传Excel进行分班
     */
    @PreAuthorize("@ss.hasPermi('system:classes:export')")
    @PostMapping("/uploadAndUpdateByDivision")
    public AjaxResult uploadAndUpdateByDivision(MultipartFile file) throws IOException {
        String string = classesService.uploadAndUpdateByDivision(file);
        switch (string) {
            case "5":
                return AjaxResult.error("Excel表格中不可以出现为空的单元格,请检查");
            case "4":
                return AjaxResult.error("上传文件失败,请检查格式,文件需为.xlsx类型");
            case "3":
                return AjaxResult.error("导入的Excel模板与下载的不匹配,请重新上传");
            case "2":
                return AjaxResult.error("未从表格中读取到数据");
            case "1":
                return toAjax(1);
            default:
                return AjaxResult.error("系统中找不到关于 " + string + " 的学生信息,请核对Excel表格");
        }
    }

    /**
     * 查询班级对应学生列表
     * @param gid
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:classes:list')")
    @GetMapping("/selectOneToMoreClass")
    public AjaxResult selectOneToMoreClass(Integer gid) {
        return AjaxResult.success(classesService.selectOneToMoreClass(gid));
    }

    /**
     * 导出班级列表
     */
    @PreAuthorize("@ss.hasPermi('system:classes:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classes classes)
    {
        List<Classes> list = classesService.selectClassesList(classes);
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:classes:query')")
    @GetMapping(value = "/{cid}")
    public AjaxResult getInfo(@PathVariable("cid") Long cid)
    {
        return success(classesService.selectClassesByCid(cid));
    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('system:classes:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classes classes)
    {
        return toAjax(classesService.insertClasses(classes));
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('system:classes:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classes classes)
    {
        return toAjax(classesService.updateClasses(classes));
    }

    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('system:classes:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cids}")
    public AjaxResult remove(@PathVariable Long[] cids)
    {
        return toAjax(classesService.deleteClassesByCids(cids));
    }
}
